﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using SAM_Edu.entity;
using System.Drawing;
using System.IO;


namespace SAM_Edu.dao
{
    public class daoUser
    {


        static MySqlConnection conn;
        static User user;
        public static User getUserDetails(int uid)  // havent think of a logic yet..
        {

            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "Select * FROM user INNER JOIN accounts ON user.userid =accounts.userid WHERE accounts.userid ='" + uid + "'";
            MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader2;
            conn.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            while (MyReader2.Read())
            {

                string fname = MyReader2["Fullname"].ToString();
                string email = MyReader2["Email"].ToString();
                int countrycode = int.Parse(MyReader2["CountryCode"].ToString());
                int areacode = int.Parse(MyReader2["AreaCode"].ToString());
                int mobileNo = int.Parse(MyReader2["MobileNumber"].ToString());
                byte[] img = (byte[])MyReader2["Photo"];

                MemoryStream ms = new MemoryStream(img);
                Image returnImage = Image.FromStream(ms);


                user = new User(fname, "", "", "", email, countrycode, areacode, mobileNo, uid, returnImage);
            }
            conn.Close();
            return user;
        }
        public static bool UpdateUserDetails(string fullname, string email, int ccode, int areacode, int mobileno, int uid)
        {

            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "UPDATE user SET Fullname ='" + fullname + "', Email = '" + email + "' , CountryCode = '" + ccode + "' , AreaCode = '" + areacode + "' , MobileNumber = '" + mobileno + "' WHERE userid = '" + uid + "'";
            MySqlCommand cmd = new MySqlCommand(Query, conn);
            conn.Open();
            int result = cmd.ExecuteNonQuery();

            if (result > 0)
            {

                return true;
            }
            else
            {
                return false;
            }
            conn.Close();
        }
        public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();

            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();

        }
        public static int CreateUser(User cr)
        {
            int result = 0;
            conn = DatabaseConnection.getConnection();
            byte[] imgbyte = ImageToByteArray(cr.Photo);

            string Query = "insert into user(Fullname,Email,CountryCode,AreaCode,MobileNumber,Photo) values('" + (cr.FirstName + " " + cr.MiddleName + " " + cr.LastName + " ( " + cr.OtherName + " ) ") + "','" + cr.Email + "','" + cr.CountryCode + "','" + cr.AreaCode + "','" + cr.MobileNumber+ "','" + imgbyte + "');Select LAST_INSERT_ID();";
            MySqlCommand cmd = new MySqlCommand(Query, conn);

            conn.Open();
            // cmd.ExecuteNonQuery();
            result = int.Parse(cmd.ExecuteScalar().ToString());


            conn.Close();
            return result;
        }

    }
}

